package com.testword.controller;

import com.testword.entity.dto.UserLoginInforDTO;
import com.testword.entity.po.UserInfo;
import com.testword.entity.query.UserInfoQuery;
import com.testword.entity.vo.ResponseVO;
import com.testword.entity.vo.Result;
import com.testword.service.UserInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 学生信息表
Controller
 */
@RestController
@RequestMapping("/userInfo")
@CrossOrigin(origins = "*",maxAge = 3600)
public class UserInfoController extends ABaseController{

	private static final Logger logger = LoggerFactory.getLogger(UserInfoController.class);

	@Resource
	private UserInfoService userInfoService;

	public static final String SESSION_KEY = "session_key";
	/**
	 * 分页查询，返回ResponseVO
	 */
	@RequestMapping("/loadDataList")
	public ResponseVO loadDataList(UserInfoQuery query) {
		return getSuccessResponseVO(userInfoService.findListByPage(query));
	}

	/**
	 * 新增
	 */
	@RequestMapping("/add")
	public ResponseVO add(UserInfo bean) {
		this.userInfoService.add(bean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 批量新增
	 */
	@RequestMapping("/addBatch")
	public ResponseVO addBatch(@RequestBody List<UserInfo> listBean) {
		this.userInfoService.addBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 批量新增/修改
	 */
	@RequestMapping("/addOrUpdateBatch")
	public ResponseVO addOrUpdateBatch(@RequestBody List<UserInfo> listBean) {
		this.userInfoService.addOrUpdateBatch(listBean);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据StudentId查询
	 */
	@RequestMapping("/getUserInfoByStudentId")
	public ResponseVO getUserInfoByStudentId(String studentId) {
		return getSuccessResponseVO(this.userInfoService.getUserInfoByStudentId(studentId));
	}

	/**
	 * 根据StudentId修改
	 */
	@RequestMapping("/updateUserInfoByStudentId")
	public ResponseVO updateUserInfoByStudentId(UserInfo bean, String studentId) {
		this.userInfoService.updateUserInfoByStudentId(bean,studentId);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据StudentId删除
	 */
	@RequestMapping("/deleteUserInfoByStudentId")
	public ResponseVO deleteUserInfoByStudentId(String studentId) {
		this.userInfoService.deleteUserInfoByStudentId(studentId);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据NickName查询
	 */
	@RequestMapping("/getUserInfoByNickName")
	public ResponseVO getUserInfoByNickName(String nickName) {
		return getSuccessResponseVO(this.userInfoService.getUserInfoByNickName(nickName));
	}

	/**
	 * 根据NickName修改
	 */
	@RequestMapping("/updateUserInfoByNickName")
	public ResponseVO updateUserInfoByNickName(UserInfo bean, String nickName) {
		this.userInfoService.updateUserInfoByNickName(bean,nickName);
		return getSuccessResponseVO(null);
	}

	/**
	 * 根据NickName删除
	 */
	@RequestMapping("/deleteUserInfoByNickName")
	public ResponseVO deleteUserInfoByNickName(String nickName) {
		this.userInfoService.deleteUserInfoByNickName(nickName);
		return getSuccessResponseVO(null);
	}

	@RequestMapping("/login")
	public ResponseVO login(HttpSession session,String studentId,String nickName,Integer cet4Score,Integer cet6Score){
		UserInfo userInfo = userInfoService.login(studentId,nickName,cet4Score,cet6Score);

		return getSuccessResponseVO(userInfo);
	}

	@PostMapping("/register")
	public Result register( UserLoginInforDTO userLoginInforDTO) {
		userInfoService.register(userLoginInforDTO);
		return  Result.success();
	}


}